import { Component,Input,OnChanges, OnInit, OnDestroy } from '@angular/core';

import { Util } from '../../../../utils/util';

import { Scope } from '../../../../utils/emit';

import { ShopHistorySrvice } from './shop.history.service';

@Component({
  selector: 'shop-history',
  templateUrl: './shop.history.html',
  providers: [ShopHistorySrvice]
})

export class ShopHistoryComponent {

  @Input() date:any;
  shopInfo: any;
  constructor(private service: ShopHistorySrvice, private util: Util,private scope:Scope) {

    this.shopInfo = this.util.sessionStore().get('shopDetail');

  }

  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['date', 'monthSellMoney', 'monthSellAmount', 'sellMoney', 'sellAmount', 'productCount', 'shopType', 'uid'],
      dimText: ['日期', '近30天销售额', '近30天销售量', '日销售额', '日销售量', '动销宝贝数', '店铺类型', '店铺id']
    },
    "type": 'line-mm'
  };

  columns = ['日期', '日销售量', '日销售额', '近30天销售量', '近30天销售额', '动销宝贝数'];


  eventOff:any;
  ngOnInit() {

    // 日销，月销
    this.eventOff = this.scope.on("sellTimeType", (event) => {


      if (this.vm.loading) {
        this.offEvent.unsubscribe();
      }

      this.vm.loading = true;

      this.offEvent = this.service.get(this.httpParam).subscribe((res: any) => {

        this.vm.data = res.data;

        this.vm.loading = false;

      }, () => { });

    });
  }

 // change

  ngOnChanges(changes) {

    console.log(changes);

    if (changes.date.currentValue && changes.date.currentValue.timeType =='day') {

      console.log(this.date);
      this.vm.loading = true;
      this.getHttp(this.shopInfo,this.date.date);
    }


  }


  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();

    }

    if(this.eventOff){
      this.eventOff.unsubscribe();
    }
  }


  export() {
    console.log("export");

    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim, this.vm.column.dimText);


    this.util.export(data, this.vm.column.fileName);
  }

  sellResulet:any = {};
  httpParam:any;
  offEvent: any;
  getHttp(shopInfo, date) {

    if(this.vm.loading && this.offEvent){
      this.offEvent.unsubscribe();
    }

    this.sellResulet = {
      'sellMoney':0,
      'sellAmount':0
    };

    let json = {
      uid: shopInfo.uid
    };

    Object.assign(json, date);
    this.httpParam = json;

    var res = {"code":0,"msg":"成功","data":[{"id":"725677994\u00012017-11-07 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":136729805,"monthSellMoney":3.56309185115E9,"sellAmount":3814931,"sellMoney":9.753141785E7,"productCount":26775,"modifyTime":null,"ifashion":null,"date":"2017-11-07 00:00:00","reason":null},{"id":"725677994\u00012017-11-08 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":94355232,"monthSellMoney":4.22901023735E9,"sellAmount":4362336,"sellMoney":1.6670454741E8,"productCount":29478,"modifyTime":null,"ifashion":null,"date":"2017-11-08 00:00:00","reason":null},{"id":"725677994\u00012017-11-09 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":95976346,"monthSellMoney":4.40159853549E9,"sellAmount":3193945,"sellMoney":1.1489783299E8,"productCount":32020,"modifyTime":null,"ifashion":null,"date":"2017-11-09 00:00:00","reason":null},{"id":"725677994\u00012017-11-10 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":100833969,"monthSellMoney":3.14505742071E9,"sellAmount":1695239,"sellMoney":4.01710757E7,"productCount":33306,"modifyTime":null,"ifashion":null,"date":"2017-11-10 00:00:00","reason":null},{"id":"725677994\u00012017-11-11 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":105324975,"monthSellMoney":3.40517533846E9,"sellAmount":23984006,"sellMoney":1.0044638719E9,"productCount":35480,"modifyTime":null,"ifashion":null,"date":"2017-11-11 00:00:00","reason":null},{"id":"725677994\u00012017-11-12 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":105368966,"monthSellMoney":3.40937837919E9,"sellAmount":12583660,"sellMoney":4.2914310449E8,"productCount":36122,"modifyTime":null,"ifashion":null,"date":"2017-11-12 00:00:00","reason":null},{"id":"725677994\u00012017-11-13 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":105657981,"monthSellMoney":3.39918935998E9,"sellAmount":6833668,"sellMoney":1.9802797832E8,"productCount":32134,"modifyTime":null,"ifashion":null,"date":"2017-11-13 00:00:00","reason":null},{"id":"725677994\u00012017-11-14 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":106207937,"monthSellMoney":3.42048414485E9,"sellAmount":3315100,"sellMoney":8.283934356E7,"productCount":33125,"modifyTime":null,"ifashion":null,"date":"2017-11-14 00:00:00","reason":null},{"id":"725677994\u00012017-11-15 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":106640517,"monthSellMoney":3.43304937928E9,"sellAmount":3354229,"sellMoney":8.473073433E7,"productCount":33220,"modifyTime":null,"ifashion":null,"date":"2017-11-15 00:00:00","reason":null},{"id":"725677994\u00012017-11-16 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":106962527,"monthSellMoney":3.44567486697E9,"sellAmount":3176792,"sellMoney":8.382244989E7,"productCount":35554,"modifyTime":null,"ifashion":null,"date":"2017-11-16 00:00:00","reason":null},{"id":"725677994\u00012017-11-17 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107012622,"monthSellMoney":3.44322660653E9,"sellAmount":3277146,"sellMoney":8.745716659E7,"productCount":33792,"modifyTime":null,"ifashion":null,"date":"2017-11-17 00:00:00","reason":null},{"id":"725677994\u00012017-11-18 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107226069,"monthSellMoney":3.45295113988E9,"sellAmount":3068046,"sellMoney":8.226373558E7,"productCount":34372,"modifyTime":null,"ifashion":null,"date":"2017-11-18 00:00:00","reason":null},{"id":"725677994\u00012017-11-19 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107395291,"monthSellMoney":3.46310157795E9,"sellAmount":3230321,"sellMoney":8.549853425E7,"productCount":35976,"modifyTime":null,"ifashion":null,"date":"2017-11-19 00:00:00","reason":null},{"id":"725677994\u00012017-11-20 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107326251,"monthSellMoney":3.44891934784E9,"sellAmount":3708154,"sellMoney":9.554035667E7,"productCount":32227,"modifyTime":null,"ifashion":null,"date":"2017-11-20 00:00:00","reason":null},{"id":"725677994\u00012017-11-21 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107791858,"monthSellMoney":3.474777744E9,"sellAmount":3424733,"sellMoney":8.954054364E7,"productCount":35633,"modifyTime":null,"ifashion":null,"date":"2017-11-21 00:00:00","reason":null},{"id":"725677994\u00012017-11-22 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":107948398,"monthSellMoney":3.48099030426E9,"sellAmount":3723786,"sellMoney":1.0617540228E8,"productCount":35519,"modifyTime":null,"ifashion":null,"date":"2017-11-22 00:00:00","reason":null},{"id":"725677994\u00012017-11-23 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108482881,"monthSellMoney":3.51012670369E9,"sellAmount":3327745,"sellMoney":9.475592273E7,"productCount":38034,"modifyTime":null,"ifashion":null,"date":"2017-11-23 00:00:00","reason":null},{"id":"725677994\u00012017-11-24 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108349281,"monthSellMoney":3.49723815646E9,"sellAmount":3157891,"sellMoney":8.716428741E7,"productCount":34403,"modifyTime":null,"ifashion":null,"date":"2017-11-24 00:00:00","reason":null},{"id":"725677994\u00012017-11-25 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108141019,"monthSellMoney":3.48556061119E9,"sellAmount":3245454,"sellMoney":8.620612957E7,"productCount":32263,"modifyTime":null,"ifashion":null,"date":"2017-11-25 00:00:00","reason":null},{"id":"725677994\u00012017-11-26 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108756770,"monthSellMoney":3.52042922758E9,"sellAmount":3338971,"sellMoney":8.990155387E7,"productCount":36487,"modifyTime":null,"ifashion":null,"date":"2017-11-26 00:00:00","reason":null},{"id":"725677994\u00012017-11-27 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108784628,"monthSellMoney":3.51444567191E9,"sellAmount":4053558,"sellMoney":1.1561259476E8,"productCount":34337,"modifyTime":null,"ifashion":null,"date":"2017-11-27 00:00:00","reason":null},{"id":"725677994\u00012017-11-28 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":108705916,"monthSellMoney":3.50533962686E9,"sellAmount":3658336,"sellMoney":9.972612193E7,"productCount":32327,"modifyTime":null,"ifashion":null,"date":"2017-11-28 00:00:00","reason":null},{"id":"725677994\u00012017-11-29 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":109535972,"monthSellMoney":3.55224427693E9,"sellAmount":3893342,"sellMoney":1.0972870938E8,"productCount":38043,"modifyTime":null,"ifashion":null,"date":"2017-11-29 00:00:00","reason":null},{"id":"725677994\u00012017-11-30 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":109193571,"monthSellMoney":3.52669614937E9,"sellAmount":3674325,"sellMoney":9.871273624E7,"productCount":32676,"modifyTime":null,"ifashion":null,"date":"2017-11-30 00:00:00","reason":null},{"id":"725677994\u00012017-12-01 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":109657420,"monthSellMoney":3.54971195684E9,"sellAmount":3560078,"sellMoney":9.545275267E7,"productCount":34849,"modifyTime":null,"ifashion":null,"date":"2017-12-01 00:00:00","reason":null},{"id":"725677994\u00012017-12-02 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":109990947,"monthSellMoney":3.57152007502E9,"sellAmount":4085659,"sellMoney":1.1383230476E8,"productCount":38163,"modifyTime":null,"ifashion":null,"date":"2017-12-02 00:00:00","reason":null},{"id":"725677994\u00012017-12-03 00:00:00","uid":"725677994","nick":null,"sid":"67597230","shopType":"商城","shopName":null,"shopCreateTime":null,"mainCategory":null,"shopLevel":null,"loc":null,"province":null,"city":null,"monthSellAmount":109997894,"monthSellMoney":3.56287788257E9,"sellAmount":3726328,"sellMoney":9.81308616E7,"productCount":35105,"modifyTime":null,"ifashion":null,"date":"2017-12-03 00:00:00","reason":null}]};

    setTimeout(() => {

      this.vm.column.fileName = date.begin.slice(0, 10) + '~' + date.end.slice(0, 10) + this.shopInfo.shopName;

      this.vm.data = res.data;
      // 合计
      this.sellResulet = this.util.getSellAllResult(this.sellResulet, res.data);

      this.vm.loading = false;

    }, 100);

  }


}
